<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.13"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>SC2API: sc2::Connection Class Reference</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="SC2-Logo.png"/></td>
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname">SC2API
   </div>
   <div id="projectbrief">An API for AI for StarCraft II</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.13 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
$(function() {
  initMenu('',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
</script>
<div id="main-nav"></div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div id="nav-path" class="navpath">
  <ul>
<li class="navelem"><b>sc2</b></li><li class="navelem"><a class="el" href="classsc2_1_1_connection.html">Connection</a></li>  </ul>
</div>
</div><!-- top -->
<div class="header">
  <div class="summary">
<a href="#pub-methods">Public Member Functions</a> &#124;
<a href="#pub-attribs">Public Attributes</a> &#124;
<a href="classsc2_1_1_connection-members.html">List of all members</a>  </div>
  <div class="headertitle">
<div class="title">sc2::Connection Class Reference</div>  </div>
</div><!--header-->
<div class="contents">

<p><code>#include &lt;<a class="el" href="sc2__connection_8h_source.html">sc2_connection.h</a>&gt;</code></p>
<table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a name="pub-methods"></a>
Public Member Functions</h2></td></tr>
<tr class="memitem:ae2b9a0a28789ac5c6304506cf0bea4b0"><td class="memItemLeft" align="right" valign="top">bool&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="classsc2_1_1_connection.html#ae2b9a0a28789ac5c6304506cf0bea4b0">Connect</a> (const std::string &amp;address, int port, bool verbose=true)</td></tr>
<tr class="separator:ae2b9a0a28789ac5c6304506cf0bea4b0"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:af6bd305434d9efd78a6868749e764749"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="classsc2_1_1_connection.html#af6bd305434d9efd78a6868749e764749">Send</a> (const SC2APIProtocol::Request *request)</td></tr>
<tr class="separator:af6bd305434d9efd78a6868749e764749"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a383d7968897ccc2e35c2716bb110584f"><td class="memItemLeft" align="right" valign="top">bool&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="classsc2_1_1_connection.html#a383d7968897ccc2e35c2716bb110584f">Receive</a> (SC2APIProtocol::Response *&amp;response, unsigned int timeout_ms)</td></tr>
<tr class="separator:a383d7968897ccc2e35c2716bb110584f"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:af2d5cf1aff2fef4d30ddd9ab256419ed"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="classsc2_1_1_connection.html#af2d5cf1aff2fef4d30ddd9ab256419ed">PopResponse</a> (SC2APIProtocol::Response *&amp;response)</td></tr>
<tr class="separator:af2d5cf1aff2fef4d30ddd9ab256419ed"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a1a3d3c532703e0bf7a88f49ee22aa65e"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="classsc2_1_1_connection.html#a1a3d3c532703e0bf7a88f49ee22aa65e">SetTimeoutCallback</a> (std::function&lt; void()&gt; callback)</td></tr>
<tr class="separator:a1a3d3c532703e0bf7a88f49ee22aa65e"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a1f2556582badc5f98b349efa17d823cd"><td class="memItemLeft" align="right" valign="top"><a id="a1f2556582badc5f98b349efa17d823cd"></a>
void&#160;</td><td class="memItemRight" valign="bottom"><b>SetConnectionClosedCallback</b> (std::function&lt; void()&gt; callback)</td></tr>
<tr class="separator:a1f2556582badc5f98b349efa17d823cd"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a69ca71e72ab2a0a1bad36fc729fedc02"><td class="memItemLeft" align="right" valign="top">bool&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="classsc2_1_1_connection.html#a69ca71e72ab2a0a1bad36fc729fedc02">HasConnection</a> () const</td></tr>
<tr class="separator:a69ca71e72ab2a0a1bad36fc729fedc02"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a7c06854ac5724952f69fb229a3defcee"><td class="memItemLeft" align="right" valign="top">bool&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="classsc2_1_1_connection.html#a7c06854ac5724952f69fb229a3defcee">PollResponse</a> ()</td></tr>
<tr class="separator:a7c06854ac5724952f69fb229a3defcee"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:aad919078849b1b85b38db8d8d11c8004"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="classsc2_1_1_connection.html#aad919078849b1b85b38db8d8d11c8004">PushResponse</a> (SC2APIProtocol::Response *&amp;response)</td></tr>
<tr class="separator:aad919078849b1b85b38db8d8d11c8004"><td class="memSeparator" colspan="2">&#160;</td></tr>
</table><table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a name="pub-attribs"></a>
Public Attributes</h2></td></tr>
<tr class="memitem:abab9f14cde0fe589fc3a1c9588c6607a"><td class="memItemLeft" align="right" valign="top"><a id="abab9f14cde0fe589fc3a1c9588c6607a"></a>
std::function&lt; void()&gt;&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="classsc2_1_1_connection.html#abab9f14cde0fe589fc3a1c9588c6607a">timeout_callback_</a></td></tr>
<tr class="memdesc:abab9f14cde0fe589fc3a1c9588c6607a"><td class="mdescLeft">&#160;</td><td class="mdescRight">Timeout callback. <br /></td></tr>
<tr class="separator:abab9f14cde0fe589fc3a1c9588c6607a"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a4df89365bfaec33df14cf9a84f42c938"><td class="memItemLeft" align="right" valign="top"><a id="a4df89365bfaec33df14cf9a84f42c938"></a>
std::function&lt; void()&gt;&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="classsc2_1_1_connection.html#a4df89365bfaec33df14cf9a84f42c938">connection_closed_callback_</a></td></tr>
<tr class="memdesc:a4df89365bfaec33df14cf9a84f42c938"><td class="mdescLeft">&#160;</td><td class="mdescRight">Timeout callback. <br /></td></tr>
<tr class="separator:a4df89365bfaec33df14cf9a84f42c938"><td class="memSeparator" colspan="2">&#160;</td></tr>
</table>
<a name="details" id="details"></a><h2 class="groupheader">Detailed Description</h2>
<div class="textblock"><p>This class acts as a wrapper around a websocket connection and queue responsible for both sending out and receiving protobuf messages. </p>
</div><h2 class="groupheader">Member Function Documentation</h2>
<a id="ae2b9a0a28789ac5c6304506cf0bea4b0"></a>
<h2 class="memtitle"><span class="permalink"><a href="#ae2b9a0a28789ac5c6304506cf0bea4b0">&#9670;&nbsp;</a></span>Connect()</h2>

<div class="memitem">
<div class="memproto">
      <table class="memname">
        <tr>
          <td class="memname">bool sc2::Connection::Connect </td>
          <td>(</td>
          <td class="paramtype">const std::string &amp;&#160;</td>
          <td class="paramname"><em>address</em>, </td>
        </tr>
        <tr>
          <td class="paramkey"></td>
          <td></td>
          <td class="paramtype">int&#160;</td>
          <td class="paramname"><em>port</em>, </td>
        </tr>
        <tr>
          <td class="paramkey"></td>
          <td></td>
          <td class="paramtype">bool&#160;</td>
          <td class="paramname"><em>verbose</em> = <code>true</code>&#160;</td>
        </tr>
        <tr>
          <td></td>
          <td>)</td>
          <td></td><td></td>
        </tr>
      </table>
</div><div class="memdoc">
<p>Connects via websocket on a given address/port. </p><dl class="params"><dt>Parameters</dt><dd>
  <table class="params">
    <tr><td class="paramname">address</td><td>The address to connect to, will most commonly be used locally so 127.0.0.1. </td></tr>
    <tr><td class="paramname">port</td><td>The port to connect the, the default for s2api is 9168 unless specified otherwise in settings. </td></tr>
  </table>
  </dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>Returns true if the connection was successful and false otherwise. </dd></dl>

</div>
</div>
<a id="a69ca71e72ab2a0a1bad36fc729fedc02"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a69ca71e72ab2a0a1bad36fc729fedc02">&#9670;&nbsp;</a></span>HasConnection()</h2>

<div class="memitem">
<div class="memproto">
      <table class="memname">
        <tr>
          <td class="memname">bool sc2::Connection::HasConnection </td>
          <td>(</td>
          <td class="paramname"></td><td>)</td>
          <td> const</td>
        </tr>
      </table>
</div><div class="memdoc">
<p>Whether or not the connection is valid. </p><dl class="section return"><dt>Returns</dt><dd>true if the connection is valid, false otherwise. </dd></dl>

</div>
</div>
<a id="a7c06854ac5724952f69fb229a3defcee"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a7c06854ac5724952f69fb229a3defcee">&#9670;&nbsp;</a></span>PollResponse()</h2>

<div class="memitem">
<div class="memproto">
      <table class="memname">
        <tr>
          <td class="memname">bool sc2::Connection::PollResponse </td>
          <td>(</td>
          <td class="paramname"></td><td>)</td>
          <td></td>
        </tr>
      </table>
</div><div class="memdoc">
<p>Polls the queue in a thread safe way to check if a message has been received. </p><dl class="section return"><dt>Returns</dt><dd>true if there is a response in the queue, false otherwise. </dd></dl>

</div>
</div>
<a id="af2d5cf1aff2fef4d30ddd9ab256419ed"></a>
<h2 class="memtitle"><span class="permalink"><a href="#af2d5cf1aff2fef4d30ddd9ab256419ed">&#9670;&nbsp;</a></span>PopResponse()</h2>

<div class="memitem">
<div class="memproto">
      <table class="memname">
        <tr>
          <td class="memname">void sc2::Connection::PopResponse </td>
          <td>(</td>
          <td class="paramtype">SC2APIProtocol::Response *&amp;&#160;</td>
          <td class="paramname"><em>response</em></td><td>)</td>
          <td></td>
        </tr>
      </table>
</div><div class="memdoc">
<p>PopResponse is called in the Receive function when a message has been received off of the civetweb thread. Alternatively you could poll for responses with PollResponse and consume the message manually with this function. </p><dl class="params"><dt>Parameters</dt><dd>
  <table class="params">
    <tr><td class="paramname">response</td><td>The response pointer to be filled out. </td></tr>
  </table>
  </dd>
</dl>

</div>
</div>
<a id="aad919078849b1b85b38db8d8d11c8004"></a>
<h2 class="memtitle"><span class="permalink"><a href="#aad919078849b1b85b38db8d8d11c8004">&#9670;&nbsp;</a></span>PushResponse()</h2>

<div class="memitem">
<div class="memproto">
      <table class="memname">
        <tr>
          <td class="memname">void sc2::Connection::PushResponse </td>
          <td>(</td>
          <td class="paramtype">SC2APIProtocol::Response *&amp;&#160;</td>
          <td class="paramname"><em>response</em></td><td>)</td>
          <td></td>
        </tr>
      </table>
</div><div class="memdoc">
<p>PushResponse is called by a civetweb thread when it receives a message off the socket. Pushing a response triggers a condition and enqueues a message. The condition will cause anyone currently blocking for a response (if Receive is called) to wake up and be able to consume that message. </p><dl class="params"><dt>Parameters</dt><dd>
  <table class="params">
    <tr><td class="paramname">response</td><td>A pointer to the Response to queue. </td></tr>
  </table>
  </dd>
</dl>

</div>
</div>
<a id="a383d7968897ccc2e35c2716bb110584f"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a383d7968897ccc2e35c2716bb110584f">&#9670;&nbsp;</a></span>Receive()</h2>

<div class="memitem">
<div class="memproto">
      <table class="memname">
        <tr>
          <td class="memname">bool sc2::Connection::Receive </td>
          <td>(</td>
          <td class="paramtype">SC2APIProtocol::Response *&amp;&#160;</td>
          <td class="paramname"><em>response</em>, </td>
        </tr>
        <tr>
          <td class="paramkey"></td>
          <td></td>
          <td class="paramtype">unsigned int&#160;</td>
          <td class="paramname"><em>timeout_ms</em>&#160;</td>
        </tr>
        <tr>
          <td></td>
          <td>)</td>
          <td></td><td></td>
        </tr>
      </table>
</div><div class="memdoc">
<p>Receive will block until a message is received from its websocket connection. If a message is not received within the timeout it will set response to null and return false, it also calls a timeout callback that can be used if a user has any timeout logic. </p><dl class="params"><dt>Parameters</dt><dd>
  <table class="params">
    <tr><td class="paramname">response</td><td>The response pointer to be filled out.  The max time, in milliseconds, the function will wait to receive a message. </td></tr>
  </table>
  </dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>Returns true if a message is received, false otherwise. </dd></dl>

</div>
</div>
<a id="af6bd305434d9efd78a6868749e764749"></a>
<h2 class="memtitle"><span class="permalink"><a href="#af6bd305434d9efd78a6868749e764749">&#9670;&nbsp;</a></span>Send()</h2>

<div class="memitem">
<div class="memproto">
      <table class="memname">
        <tr>
          <td class="memname">void sc2::Connection::Send </td>
          <td>(</td>
          <td class="paramtype">const SC2APIProtocol::Request *&#160;</td>
          <td class="paramname"><em>request</em></td><td>)</td>
          <td></td>
        </tr>
      </table>
</div><div class="memdoc">
<p>Sends a request via the websocket connection. This function assumes Connect has been called and returned success. It will assert in debug if that's not the case and will early out in a build that doesn't have asserts built in. This function also allocates a byte buffer to accommodate the request, it frees that buffer before returning. </p><dl class="params"><dt>Parameters</dt><dd>
  <table class="params">
    <tr><td class="paramname">request</td><td>A pointer to the Request object. </td></tr>
  </table>
  </dd>
</dl>

</div>
</div>
<a id="a1a3d3c532703e0bf7a88f49ee22aa65e"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a1a3d3c532703e0bf7a88f49ee22aa65e">&#9670;&nbsp;</a></span>SetTimeoutCallback()</h2>

<div class="memitem">
<div class="memproto">
      <table class="memname">
        <tr>
          <td class="memname">void sc2::Connection::SetTimeoutCallback </td>
          <td>(</td>
          <td class="paramtype">std::function&lt; void()&gt;&#160;</td>
          <td class="paramname"><em>callback</em></td><td>)</td>
          <td></td>
        </tr>
      </table>
</div><div class="memdoc">
<p>An accessor function that a user can bind a timeout function to. </p><dl class="params"><dt>Parameters</dt><dd>
  <table class="params">
    <tr><td class="paramname">callback</td><td>A functor or lambda that represents the callback. </td></tr>
  </table>
  </dd>
</dl>

</div>
</div>
<hr/>The documentation for this class was generated from the following file:<ul>
<li>include/sc2api/<a class="el" href="sc2__connection_8h_source.html">sc2_connection.h</a></li>
</ul>
</div><!-- contents -->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/>
</a> 1.8.13
</small></address>
</body>
</html>
